﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SwinGame;
using System.Drawing;

namespace SimpleSurvival.src
{
    //-----------------------------------------------//
    //ENUM - UnitTeam.cs
    //-----------------------------------------------//
    //To determine if enemy or not
    public enum UnitTeam
    {
        PLAYER_UNIT,
        ENEMY_UNIT,
    }

    //-----------------------------------------------//
    //INTERFACE - IUnit.cs
    //-----------------------------------------------//
    //What other objects see
    public interface IUnit
    {
        UnitTeam Team { set;  get; }
        int Hp {  get; }
        void IsHit(IDamagePacket d, IEntity source);
        void Attack(Vector v, EntityFactory ef);
        IEntity Entity {get; }    
    }

    //-----------------------------------------------//
    //CLASS - Unit.cs
    //-----------------------------------------------//
    //Entity that moves around and can attack.
    //Has health, Damage and a team
    public class Unit : Entity, IUnit
    {
        int _hp = -1;
        int _dam = 100;
        UnitTeam _team = UnitTeam.ENEMY_UNIT;

        public int Hp
        {
            get { return _hp; }
            protected set { _hp = value; }
        }

        public UnitTeam Team
        {
            get { return _team; }
            set { _team = value; }
        }

        public IEntity Entity 
        {
            get { return (IEntity)(this); } 
        }

        int Damage 
        {
            get { return _dam; }
            set { _dam = value; }
        }

        //Shoots a projectile
        public void Attack(Vector v, EntityFactory ef)
        {
            SinePacketController lpc = new SinePacketController(v,5f,10f);
            ef.MakeDamagePacket(DamagePacketType.FIREBALL, lpc, Damage, this, 10);
        }

        //When is hit by a projectile
        public void IsHit(IDamagePacket d, IEntity damageSource)
        {
            if ((IEntity)(this) == damageSource) { throw new Exception("Unit attacked self"); }
            Hp = Hp - d.Damage;
            Console.WriteLine(this.Hp + " " + this.Name);
            //link to event to graphics sprite explosion etc...
        }

        public Unit(string name, Vector pos, Map m, EntityDrawer ed, EntityDefinitions defs) : base(name,pos,m,ed,defs)
        {
            //load values//
            CanCollide = true;
            Hp = Convert.ToInt16(defs.GetEntityDefValue(name,
                EntityDefinitions.FieldName.UNIT_HP));
        }
    }
}
